<template>
  <div style="box-sizing: border-box; width: 1200px; margin: 0 auto">
    <div style="padding: 10px 20px; background: #fff">
      余额 ： <span style="color: red"> ￥{{ $thousands2(price) }} </span>
    </div>

    <div class="container">
      <div class="item-box" v-for="(item, index) in imgList" :key="index">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <div style="width: 100%; overflow: hidden; line-height: 20px">
                <span>{{ item.username }}</span>

                <div style="float: right">
                  <div class="buy-btn" style="padding: 0 5px; float: right">
                    <el-button
                      class="buyBtn"
                      v-if="`${item.userId}` !== `${userId}`"
                      text
                      :icon="ShoppingTrolley"
                      @click="buyImg(item)"
                    >
                      购买
                    </el-button>
                  </div>
                  <div style="float: right">
                    <el-tag type="danger">
                      ￥{{ $thousands2(item.price) }}
                    </el-tag>
                  </div>
                </div>
              </div>
            </div>
          </template>
          <div>
            <img
              style="width: 100%; height: 300px; object-fit: cover"
              :src="'data:image/png;base64,' + item.img"
              alt=""
            />
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { markRaw } from 'vue';
import { ShoppingTrolley } from '@element-plus/icons-vue'
import { getByImgList, buyImg, getUserInfo } from '@/api/content.js'

export default {
  components: {
  },
  data () {
    return {
      ShoppingTrolley: markRaw(ShoppingTrolley),
      price: 0,
      imgList: [],
      userId: ''
    };
  },
  mounted () {
    this.userId = sessionStorage.getItem('userId')
    this.getImg()
    this.getBalance()
  },
  methods: {
    // 查询余额  
    getBalance () {
      getUserInfo({
        id: sessionStorage.getItem('userId')
      }).then(res => {
        if (res.success) {
          this.price = res.data[0].price
        }
      })
    },
    // 购买图片
    buyImg (row) {
      this.$confirm('是否购买此图片？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.buyImgAjax(row)
      }).catch(() => {
        // 取消逻辑
      })
    },
    buyImgAjax (row) {
      buyImg({
        userId: this.userId,
        id: row.id
      }).then(res => {
        if (res.success) {
          this.$message({
            type: 'success',
            message: '购买成功!'
          })
          this.getImg()
          this.getBalance()
        }
      })
    },
    // 获取图片
    getImg () {
      getByImgList({
        isBuy: true
      }).then(res => {
        if (res.success) {
          this.imgList = res.data
        }
      })
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  display: flex;
  flex-wrap: wrap;
  width: 1200px;
  margin: 0 auto;
  /* background: #fff; */
  gap: 10px; /* 添加元素之间的间距 */
}
.item-box {
  background: #fff;
  flex: 1;
  min-width: 250px;
  padding: 10px;
  border: 1px solid #ccc;
  box-sizing: border-box;
}
.buyBtn {
  color: red;
}
/deep/.buy-btn .el-button {
  height: 26px;
  padding: 5px;
}
</style>
